/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Blog;
import org.cx1.model.Listing;
import org.cx1.model.Photo;
import org.cx1.model.Place;
import org.cx1.model.User;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IBlogService;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IPhotoService;
import org.cx1.service.api.IPlaceService;
import org.cx1.service.api.IUserService;

/**
 *
 * @author ECH
 */
public class MPhotoService implements IPhotoService {

    @Override
    public ServiceResponse<String> create(Photo photo) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Photo entry = new Photo();
            id = UUID.randomUUID().toString();
            
            entry.setId(id);
            
            entry.setPath(photo.getPath());
            entry.setFilename(photo.getFilename());
            
            entry.setTitle(photo.getTitle());
            entry.setDescription(photo.getDescription());
            entry.setNotes(photo.getNotes());
                                    
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            MockDS.getPhotos().add(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Photo> read(String id) {
        ServiceResponse<Photo> response = new ServiceResponse<Photo>();
        Photo photo = null;       
        try {            
            List<Photo> records = MockDS.getPhotos();
            if (records != null && records.size() > 0) {
                for (Photo record : records) {
                    if (StringUtils.equalsIgnoreCase(record.getId(), id)) {
                        photo = record;
                        break;
                    }
                }
            }            
            response.setResponse(photo);
            response.setSuccessful(true);           
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<Photo>> readAll(int offset, int limit) {
        ServiceResponse<List<Photo>> response = new ServiceResponse<List<Photo>>();
        List<Photo> photos = new ArrayList<Photo>();
        try {            
            List<Photo> records = MockDS.getPhotos();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    photos.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    photos.addAll( records.subList(offset, limit) );
                }              
            }            
            response.setResponse(photos);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            List<Photo> records = MockDS.getPhotos();
            if (records != null && records.size() > 0) {
                
                for (int i = 0; i < records.size(); i++) {
                    Photo record = records.get(i);
                    
                    if (StringUtils.equalsIgnoreCase(record.getId(), id)) {
                        records.remove(i);
                        success = true;
                        break;
                    }
                }
            }            
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<String> createUserPhoto(String userID, Photo photo) {
        IUserService userService = new MUserService();
    
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            ServiceResponse<User> sr1 = userService.read(userID);            
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<String> sr2 = this.create(photo); 
                
                if (sr2.isSuccessful() && !StringUtils.isEmpty(sr2.getResponse())) {                     
                    photoID = sr2.getResponse();
                
                    User user = sr1.getResponse();
                    user.getPhotoIDs().add(photoID);
                    
                    ServiceResponse<Boolean> sr3 = userService.update(user);
                }
            }
            response.setResponse(photoID);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteUserPhoto(String userID, String photoID) {        
        IUserService userService = new MUserService();
        
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                              
            ServiceResponse<User> sr1 = userService.read(userID);                
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<Boolean> sr2 = this.delete(photoID);
                
                if (sr2.isSuccessful() && sr2.getResponse()) {                     
                                        
                    User user = sr1.getResponse();
                    user.getPhotoIDs().remove(photoID);
                    
                    ServiceResponse<Boolean> sr3 = userService.update(user);
                    success = true;
                }
            }
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<String> createListingPhoto(String listingID, Photo photo) {        
        IListingService listingService = new MListingService();
        
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            ServiceResponse<Listing> sr1 = listingService.read(listingID);            
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<String> sr2 = this.create(photo); 
                
                if (sr2.isSuccessful() && !StringUtils.isEmpty(sr2.getResponse())) {                     
                    photoID = sr2.getResponse();
                
                    Listing listing = sr1.getResponse();
                    listing.getPhotoIDs().add(photoID);                    
                    ServiceResponse<Boolean> sr3 = listingService.update(listing);
                }
            }
            response.setResponse(photoID);
            response.setSuccessful(true);           
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteListingPhoto(String listingID, String photoID) {
        IListingService listingService = new MListingService();
        
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            ServiceResponse<Listing> sr1 = listingService.read(listingID);                
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<Boolean> sr2 = this.delete(photoID);
                
                if (sr2.isSuccessful() && sr2.getResponse()) {                     
                                        
                    Listing listing = sr1.getResponse();
                    listing.getPhotoIDs().remove(photoID);                    
                    ServiceResponse<Boolean> sr3 = listingService.update(listing);
                    success = true;
                }
            }
            response.setResponse(success);
            response.setSuccessful(true);               
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<String> createPlacePhoto(String placeID, Photo photo) {
        IPlaceService placeService = new MPlaceService();
    
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            ServiceResponse<Place> sr1 = placeService.read(placeID);            
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<String> sr2 = this.create(photo); 
                
                if (sr2.isSuccessful() && !StringUtils.isEmpty(sr2.getResponse())) {                     
                    photoID = sr2.getResponse();
                
                    Place place = sr1.getResponse();
                    if (place.getPhotoIDs() == null) {
                        place.setPhotoIDs(new ArrayList<String>());
                    }
                    place.getPhotoIDs().add(photoID);                    
                    ServiceResponse<Boolean> sr3 = placeService.update(place);
                }
            }
            response.setResponse(photoID);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deletePlacePhoto(String placeID, String photoID) {
        IPlaceService placeService = new MPlaceService();
        
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            ServiceResponse<Place> sr1 = placeService.read(placeID);                
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<Boolean> sr2 = this.delete(photoID);
                
                if (sr2.isSuccessful() && sr2.getResponse()) {                     
                                        
                    Place place = sr1.getResponse();
                    place.getPhotoIDs().remove(photoID);                    
                    ServiceResponse<Boolean> sr3 = placeService.update(place);
                    success = true;
                }
            }
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<String> createBlogPhoto(String blogID, Photo photo) {
        IBlogService blogService = new MBlogService();
    
        ServiceResponse<String> response = new ServiceResponse<String>();
        String photoID = "";
        try {                  
            ServiceResponse<Blog> sr1 = blogService.read(blogID);            
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<String> sr2 = this.create(photo); 
                
                if (sr2.isSuccessful() && !StringUtils.isEmpty(sr2.getResponse())) {                     
                    photoID = sr2.getResponse();
                
                    Blog blog = sr1.getResponse();
                    if (blog.getPhotoIDs() == null) {
                        blog.setPhotoIDs(new ArrayList<String>());
                    }
                    blog.getPhotoIDs().add(photoID);                    
                    ServiceResponse<Boolean> sr3 = blogService.update(blog);
                }
            }
            response.setResponse(photoID);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteBlogPhoto(String blogID, String photoID) {
        IBlogService blogService = new MBlogService();
        
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                  
            ServiceResponse<Blog> sr1 = blogService.read(blogID);                
            if (sr1.isSuccessful() && sr1.getResponse() != null) {                
                ServiceResponse<Boolean> sr2 = this.delete(photoID);
                
                if (sr2.isSuccessful() && sr2.getResponse()) {                     
                                        
                    Blog blog = sr1.getResponse();
                    blog.getPhotoIDs().remove(photoID);                    
                    ServiceResponse<Boolean> sr3 = blogService.update(blog);
                    success = true;
                }
            }
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteListingPhoto(String listingID, String photoID, String userID) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
